package com.zhongan.gateway.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.zhongan.gateway.bean.ApiRequestLog;
import com.zhongan.gateway.mapper.ApiRequestLogMapper;
import com.zhongan.gateway.service.LogService;
import com.zhongan.gateway.web.common.PageDTO;

@Component
public class LogServiceImpl implements LogService {

    //    private static final Logger logger = LoggerFactory.getLogger(LogServiceImpl.class);
    @Resource
    private ApiRequestLogMapper apiRequestLogMapper;

    @Override
    public PageDTO<ApiRequestLog> selectPageLogs(String serviceName, String appKey, String uuid, String timeStart,
                                                 String timeEnd, Integer start, Integer limit) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (!StringUtils.isEmpty(appKey)) {
            map.put("appKey", appKey);
        }
        if (!StringUtils.isEmpty(serviceName)) {
            map.put("serviceName", serviceName);
        }
        if (!StringUtils.isEmpty(uuid)) {
            map.put("uuid", uuid);
        }
        map.put("timeStart", timeStart);
        map.put("timeEnd", timeEnd);
        int count = apiRequestLogMapper.countByParams(map);
        map.put("start", start);
        map.put("limit", limit);
        List<ApiRequestLog> logs = apiRequestLogMapper.selectPageByParams(map);
        PageDTO<ApiRequestLog> page = new PageDTO<ApiRequestLog>();
        page.setStart(start);
        page.setLimit(limit);
        page.setRows(logs);
        page.setResults(count);
        page.setHasError(false);
        return page;
    }

}
